import { AjaxCommon } from "@/libs/apiCommon"
import { PopupCommon } from "@/libs/popupCommon"
import { Utility } from "@/libs/utility.js"
import { DataAcquisition } from "@/mockData/dataAcquisition.js"
import { ManagementCenterData } from "@/mockData/managementCenterData.js"
import axios from "axios"
/**
 * 一些公用接口
 */
export class CommonApi {
  // 获取省市县
  static getProvinceCity = () => {
    return AjaxCommon(
      "/ccdc/sys/category/loadTreeRoot?async=false&pcode=B03",
      "get"
    )
  }

  // 根据用户获取用户的菜单权限
  static getUserPermissionByToken = async (module) => {
    const _t = new Date().getTime()
    let menuList = await axios.get(
      "/ccdc/sys/permission/getUserPermissionByToken?_t=" + _t,
      {
        headers: {
          Authorization: localStorage.getItem("token"),
          "X-Access-Token": localStorage.getItem("token"),
        },
      }
    )
    if (!menuList.data.success || menuList.data.result.menu.length < 1) {
      // 无权限
      PopupCommon.notify("info", "暂无权限或登录已失效!")
      return []
    }
    menuList = menuList.data.result.menu
    let menuAll = {}
    let silderData = []
    let treeData = []
    let menuListData = []
    // 数据管理中心
    if (module == "managementCenter") {
      silderData = ManagementCenterData.leftMenuData
      treeData = JSON.parse(JSON.stringify(silderData))
      menuListData = Utility.arrayFlat(treeData, (t) => t.children)
      menuAll = menuList.find(
        (t) => t.component == "managementCenter" || t.name == "managementCenter"
      )
      if (
        typeof menuAll == "undefined" ||
        typeof menuAll.meta == "undefined" ||
        typeof menuAll.children == "undefined"
      ) {
        return []
      }
    } else {
      silderData = DataAcquisition.leftMenuData
      treeData = JSON.parse(JSON.stringify(silderData))
      menuListData = Utility.arrayFlat(treeData, (t) => t.children)
      // 数据采集中心
      menuAll = menuList.find(
        (t) => t.component == "dataAcquisition" || t.name == "dataAcquisition"
      )
      if (
        typeof menuAll == "undefined" ||
        typeof menuAll.meta == "undefined" ||
        typeof menuAll.children == "undefined"
      ) {
        return []
      }
    }
    menuList = menuAll.children
    const recombination = (menu, parentId) => {
      return menu.map((t) => {
        t.title = t.meta.title
        t.menuUrl = typeof t.children == "undefined" ? t.name : ""
        var item = menuListData.filter(
          (b) => b.menuUrl == t.menuUrl && b.title == t.title
        )
        t.icon = item.length > 0 ? item[0].icon : ""
        t.parentId = parentId
        if (typeof t.children != "undefined") {
          recombination(t.children, t.id)
        }
        return t
      })
    }
    menuList = recombination(menuList, "0000")
    return menuList
  }

  // 获取当前时间差
  static async getTimeDiff() {
    let res = await axios.post("/ccdc/sys/common/getRemoteTime")
    let isTrue = false
    if (res.data.code == 200) {
      const serveTime = parseFloat(res.data.result)
      const clientTime = new Date().getTime()
      const diffTime = Math.abs(serveTime - clientTime)
      const diffSecond = diffTime / 1000
      if (30 - diffSecond < 0) {
        isTrue = true
      }
    }
    return isTrue
  }
}
